/**
 * Collection Details Page JavaScript
 * Handles individual collection management and document indexing
 */

let collectionData = null;
let documentsData = [];
let currentFilter = 'all';
let indexingPollInterval = null;

/**
 * Safe fetch wrapper with URL validation
 */
async function safeFetch(url, options = {}) {
    // Internal URLs starting with '/' are safe
    if (!url.startsWith('/')) {
        if (!URLValidator.isSafeUrl(url)) {
            throw new Error(`Blocked unsafe URL: ${url}`);
        }
    }
    return fetch(url, options);
}

/**
 * Initialize the page
 */
document.addEventListener('DOMContentLoaded', function() {
    loadCollectionDetails();

    // Setup button handlers
    document.getElementById('index-collection-btn').addEventListener('click', () => indexCollection(false));
    document.getElementById('reindex-collection-btn').addEventListener('click', () => indexCollection(true));
    document.getElementById('delete-collection-btn').addEventListener('click', deleteCollection);
    document.getElementById('cancel-indexing-btn').addEventListener('click', cancelIndexing);

    // Check if there's an active indexing task
    checkAndResumeIndexing();
});

/**
 * Load collection details and documents
 */
async function loadCollectionDetails() {
    try {
        const response = await safeFetch(URLBuilder.build(URLS.LIBRARY_API.COLLECTION_DOCUMENTS, COLLECTION_ID));
        const data = await response.json();

        if (data.success) {
            collectionData = data.collection;
            documentsData = data.documents || [];

            // Update header
            document.getElementById('collection-name').textContent = collectionData.name;
            document.getElementById('collection-description').textContent = collectionData.description || '';

            // Update statistics
            updateStatistics();

            // Display collection's embedding settings
            displayCollectionEmbeddingSettings();

            // Render documents
            renderDocuments();
        } else {
            showError('Failed to load collection details: ' + data.error);
        }
    } catch (error) {
        console.error('Error loading collection details:', error);
        showError('Failed to load collection details');
    }
}

/**
 * Update statistics
 */
function updateStatistics() {
    const totalDocs = documentsData.length;
    const indexedDocs = documentsData.filter(doc => doc.indexed).length;
    const unindexedDocs = totalDocs - indexedDocs;
    const totalChunks = documentsData.reduce((sum, doc) => sum + (doc.chunk_count || 0), 0);

    document.getElementById('stat-total-docs').textContent = totalDocs;
    document.getElementById('stat-indexed-docs').textContent = indexedDocs;
    document.getElementById('stat-unindexed-docs').textContent = unindexedDocs;
    document.getElementById('stat-total-chunks').textContent = totalChunks;
}

/**
 * Display collection's embedding settings
 */
function displayCollectionEmbeddingSettings() {
    const infoContainer = document.getElementById('collection-embedding-info');

    if (collectionData.embedding_model) {
        // Collection has stored settings - display them
        infoContainer.innerHTML = `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Provider:</span>
                <span class="ldr-info-value">${getProviderLabel(collectionData.embedding_model_type)}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Model:</span>
                <span class="ldr-info-value">${collectionData.embedding_model}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Chunk Size:</span>
                <span class="ldr-info-value">${collectionData.chunk_size || 'Not set'} ${collectionData.chunk_size ? 'characters' : ''}</span>
            </div>
            <div class="ldr-info-item">
                <span class="ldr-info-label">Chunk Overlap:</span>
                <span class="ldr-info-value">${collectionData.chunk_overlap || 'Not set'} ${collectionData.chunk_overlap ? 'characters' : ''}</span>
            </div>
            ${collectionData.embedding_dimension ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Embedding Dimension:</span>
                <span class="ldr-info-value">${collectionData.embedding_dimension}</span>
            </div>
            ` : ''}
            ${collectionData.splitter_type ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Splitter Type:</span>
                <span class="ldr-info-value">${collectionData.splitter_type}</span>
            </div>
            ` : ''}
            ${collectionData.distance_metric ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Distance Metric:</span>
                <span class="ldr-info-value">${collectionData.distance_metric}</span>
            </div>
            ` : ''}
            ${collectionData.index_type ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Index Type:</span>
                <span class="ldr-info-value">${collectionData.index_type}</span>
            </div>
            ` : ''}
            ${collectionData.normalize_vectors !== null && collectionData.normalize_vectors !== undefined ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Normalize Vectors:</span>
                <span class="ldr-info-value">${collectionData.normalize_vectors ? 'Yes' : 'No'}</span>
            </div>
            ` : ''}
            ${collectionData.index_file_size ? `
            <div class="ldr-info-item">
                <span class="ldr-info-label">Index File Size:</span>
                <span class="ldr-info-value">${collectionData.index_file_size}</span>
            </div>
            ` : ''}
        `;
    } else {
        // Collection not yet indexed - no settings stored
        infoContainer.innerHTML = `
            <div class="ldr-alert ldr-alert-info">
                <i class="fas fa-info-circle"></i> This collection hasn't been indexed yet. Settings will be stored when you index documents.
            </div>
        `;
    }
}

/**
 * Get provider label (simplified version)
 */
function getProviderLabel(providerValue) {
    const providerMap = {
        'sentence_transformers': 'Sentence Transformers',
        'ollama': 'Ollama',
        'openai': 'OpenAI',
        'anthropic': 'Anthropic',
        'cohere': 'Cohere'
    };
    return providerMap[providerValue] || providerValue || 'Not configured';
}

/**
 * Get model label (simplified version)
 */
function getModelLabel(modelValue, provider) {
    if (!modelValue) return 'Not configured';
    if (provider === 'ollama' && modelValue.includes(':')) {
        return modelValue.split(':')[0];
    }
    return modelValue;
}

/**
 * Render documents list
 */
function renderDocuments() {
    const container = document.getElementById('documents-list');
    const noDocsMessage = document.getElementById('no-documents-message');

    // Filter documents based on current filter
    let filteredDocs = documentsData;
    if (currentFilter === 'indexed') {
        filteredDocs = documentsData.filter(doc => doc.indexed);
    } else if (currentFilter === 'unindexed') {
        filteredDocs = documentsData.filter(doc => !doc.indexed);
    }

    if (filteredDocs.length === 0) {
        container.style.display = 'none';
        noDocsMessage.style.display = 'flex';
        return;
    }

    container.style.display = 'block';
    noDocsMessage.style.display = 'none';

    container.innerHTML = filteredDocs.map(doc => `
        <div class="ldr-document-item ${doc.indexed ? 'indexed' : 'unindexed'}">
            <a href="/library/document/${doc.id}" class="document-link" style="text-decoration: none; color: inherit; display: block; flex: 1;">
                <div class="ldr-document-info">
                    <div class="ldr-document-title">
                        ${escapeHtml(doc.filename)}
                        ${doc.has_pdf ? '<i class="fas fa-file-pdf" style="color: #dc3545; margin-left: 8px;" title="PDF stored"></i>' : ''}
                        ${doc.has_text_db ? '<i class="fas fa-file-alt" style="color: #28a745; margin-left: 8px;" title="Text content available"></i>' : ''}
                        ${doc.in_other_collections ? `<i class="fas fa-link" style="color: #667eea; margin-left: 8px;" title="In ${doc.other_collections_count + 1} collections"></i>` : ''}
                    </div>
                    <div class="ldr-document-meta">
                        ${doc.file_size ? `Size: ${formatBytes(doc.file_size)} • ` : ''}
                        ${doc.source_type && doc.source_type !== 'unknown' ? `<span class="badge badge-info">${doc.source_type.replace('_', ' ')}</span> • ` : ''}
                        ${doc.indexed ?
                            `<span class="badge badge-success">Indexed (${doc.chunk_count} chunks)</span>` :
                            '<span class="badge badge-warning">Not indexed</span>'
                        }
                        ${doc.last_indexed_at ? ` • Last indexed: ${new Date(doc.last_indexed_at).toLocaleString()}` : ''}
                    </div>
                </div>
            </a>
            <div class="ldr-document-actions">
                <button class="ldr-btn-remove-from-collection" onclick="event.stopPropagation(); removeDocumentFromCollection('${doc.id}')"
                        title="Remove from collection. ${doc.in_other_collections ? 'Document exists in other collections.' : 'Document will be deleted (not in other collections).'}">
                    <i class="fas fa-unlink"></i>
                </button>
                <button class="ldr-btn-delete-doc" onclick="event.stopPropagation(); deleteDocumentCompletely('${doc.id}')"
                        title="Permanently delete this document, including PDF and text content. This cannot be undone.">
                    <i class="fas fa-trash"></i>
                </button>
            </div>
        </div>
    `).join('');
}

/**
 * Filter documents
 */
function filterDocuments(filter) {
    currentFilter = filter;

    // Update button states
    document.querySelectorAll('.ldr-filter-controls .btn').forEach(btn => {
        btn.classList.remove('active');
    });
    event.target.classList.add('active');

    renderDocuments();
}


/**
 * Index collection documents (background indexing)
 */
async function indexCollection(forceReindex) {
    console.log('Index Collection button clicked, force_reindex:', forceReindex);

    const action = forceReindex ? 're-index' : 'index';
    if (!confirm(`${action.charAt(0).toUpperCase() + action.slice(1)} all documents in this collection?`)) {
        return;
    }

    try {
        const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';

        // Start background indexing
        const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/start`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify({ force_reindex: forceReindex })
        });

        const data = await response.json();

        if (!data.success) {
            if (response.status === 409) {
                // Already indexing
                showError(data.error || 'Indexing is already in progress');
                showProgressUI();
                startPolling();
            } else {
                showError(data.error || 'Failed to start indexing');
            }
            return;
        }

        console.log('Background indexing started, task_id:', data.task_id);

        // Show progress UI and start polling
        showProgressUI();
        addLogEntry('Indexing started in background...', 'info');
        startPolling();

    } catch (error) {
        console.error('Error starting indexing:', error);
        showError('Failed to start indexing');
    }
}

/**
 * Check if there's an active indexing task and resume UI
 */
async function checkAndResumeIndexing() {
    try {
        const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/status`);
        const data = await response.json();

        if (data.status === 'processing') {
            console.log('Active indexing task found, resuming UI');
            showProgressUI();
            updateProgressFromStatus(data);
            startPolling();
        }
    } catch (error) {
        console.error('Error checking indexing status:', error);
    }
}

/**
 * Show the progress UI
 */
function showProgressUI() {
    const progressSection = document.getElementById('indexing-progress');
    const cancelBtn = document.getElementById('cancel-indexing-btn');
    const indexBtn = document.getElementById('index-collection-btn');
    const reindexBtn = document.getElementById('reindex-collection-btn');

    progressSection.style.display = 'block';
    cancelBtn.style.display = 'inline-block';
    indexBtn.disabled = true;
    reindexBtn.disabled = true;
}

/**
 * Hide the progress UI
 */
function hideProgressUI() {
    const progressSection = document.getElementById('indexing-progress');
    const cancelBtn = document.getElementById('cancel-indexing-btn');
    const indexBtn = document.getElementById('index-collection-btn');
    const reindexBtn = document.getElementById('reindex-collection-btn');
    const spinner = document.getElementById('indexing-spinner');

    cancelBtn.style.display = 'none';
    indexBtn.disabled = false;
    reindexBtn.disabled = false;

    // Keep progress visible for a few seconds before hiding
    setTimeout(() => {
        progressSection.style.display = 'none';
    }, 5000);
}

/**
 * Start polling for indexing status
 */
function startPolling() {
    // Clear any existing interval
    if (indexingPollInterval) {
        clearInterval(indexingPollInterval);
    }

    // Poll every 2 seconds
    indexingPollInterval = setInterval(async () => {
        try {
            const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/status`);
            const data = await response.json();

            updateProgressFromStatus(data);

            // Stop polling if indexing is done
            if (['completed', 'failed', 'cancelled', 'idle'].includes(data.status)) {
                clearInterval(indexingPollInterval);
                indexingPollInterval = null;

                if (data.status === 'completed') {
                    addLogEntry(data.progress_message || 'Indexing completed!', 'success');
                } else if (data.status === 'failed') {
                    addLogEntry(`Indexing failed: ${data.error_message || 'Unknown error'}`, 'error');
                } else if (data.status === 'cancelled') {
                    addLogEntry('Indexing was cancelled', 'warning');
                }

                hideProgressUI();
                loadCollectionDetails();
            }
        } catch (error) {
            console.error('Error polling status:', error);
        }
    }, 2000);
}

/**
 * Update progress UI from status data
 */
function updateProgressFromStatus(data) {
    const progressFill = document.getElementById('progress-fill');
    const progressText = document.getElementById('progress-text');

    if (data.progress_total > 0) {
        const percent = Math.round((data.progress_current / data.progress_total) * 100);
        progressFill.style.width = percent + '%';
    }

    if (data.progress_message) {
        progressText.textContent = data.progress_message;
    }
}

/**
 * Cancel indexing
 */
async function cancelIndexing() {
    if (!confirm('Cancel the current indexing operation?')) {
        return;
    }

    try {
        const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';

        const response = await safeFetch(`/library/api/collections/${COLLECTION_ID}/index/cancel`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();

        if (data.success) {
            const progressText = document.getElementById('progress-text');
            progressText.textContent = 'Cancelling...';
            addLogEntry('Cancellation requested...', 'warning');
        } else {
            showError(data.error || 'Failed to cancel indexing');
        }
    } catch (error) {
        console.error('Error cancelling indexing:', error);
        showError('Failed to cancel indexing');
    }
}

/**
 * Add log entry to progress log
 */
function addLogEntry(message, type = 'info') {
    const progressLog = document.getElementById('progress-log');
    const entry = document.createElement('div');
    entry.className = `ldr-log-entry ldr-log-${type}`;
    entry.textContent = message;
    progressLog.appendChild(entry);
    progressLog.scrollTop = progressLog.scrollHeight;
}

/**
 * Delete collection
 */
async function deleteCollection() {
    if (!confirm(`Are you sure you want to delete "${collectionData.name}"? This action cannot be undone.`)) return;

    try {
        const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
        const response = await safeFetch(URLBuilder.build(URLS.LIBRARY_API.COLLECTION_DETAILS, COLLECTION_ID), {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();
        if (data.success) {
            showSuccess(`Collection "${collectionData.name}" deleted successfully`);
            // Redirect to collections page
            setTimeout(() => {
                window.location.href = '/library/collections';
            }, 1000);
        } else {
            showError('Failed to delete collection: ' + data.error);
        }
    } catch (error) {
        console.error('Error deleting collection:', error);
        showError('Failed to delete collection');
    }
}

/**
 * Format bytes to human readable
 */
function formatBytes(bytes) {
    if (bytes === 0) return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}

/**
 * Escape HTML to prevent XSS
 */
function escapeHtml(text) {
    if (!text) return '';
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

/**
 * Show success message
 */
function showSuccess(message) {
    alert('Success: ' + message);
}

/**
 * Show error message
 */
function showError(message) {
    alert('Error: ' + message);
}

/**
 * Remove document from this collection
 * If not in other collections, the document will be deleted
 */
async function removeDocumentFromCollection(documentId) {
    // Check if DeleteManager is available
    if (typeof DeleteManager !== 'undefined' && DeleteManager.removeFromCollection) {
        DeleteManager.removeFromCollection(documentId, COLLECTION_ID, {
            onSuccess: () => loadCollectionDetails()
        });
    } else {
        // Fallback to simple confirm
        if (!confirm('Remove this document from the collection? If not in other collections, it will be deleted.')) {
            return;
        }

        try {
            const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
            const response = await fetch(`/library/api/collection/${COLLECTION_ID}/document/${documentId}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                const message = data.document_deleted
                    ? 'Document removed and deleted (not in other collections)'
                    : 'Document removed from collection';
                showSuccess(message);
                loadCollectionDetails();
            } else {
                showError('Failed to remove document: ' + data.error);
            }
        } catch (error) {
            console.error('Error removing document:', error);
            showError('Failed to remove document');
        }
    }
}

/**
 * Delete document completely (from all collections)
 */
async function deleteDocumentCompletely(documentId) {
    // Check if DeleteManager is available
    if (typeof DeleteManager !== 'undefined' && DeleteManager.deleteDocument) {
        DeleteManager.deleteDocument(documentId, {
            onSuccess: () => loadCollectionDetails()
        });
    } else {
        // Fallback to simple confirm
        if (!confirm('Permanently delete this document? This cannot be undone.')) {
            return;
        }

        try {
            const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
            const response = await fetch(`/library/api/document/${documentId}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRFToken': csrfToken
                }
            });

            const data = await response.json();
            if (data.success) {
                showSuccess('Document deleted successfully');
                loadCollectionDetails();
            } else {
                showError('Failed to delete document: ' + data.error);
            }
        } catch (error) {
            console.error('Error deleting document:', error);
            showError('Failed to delete document');
        }
    }
}
